package io.ent.modules.product.controller;

import io.ent.common.utils.PageUtils;
import io.ent.common.utils.R;
import io.ent.common.validator.ValidatorUtils;
import io.ent.modules.product.entity.ProductTypeEntity;
import io.ent.modules.product.po.ProductTypeEntityIfPo;
import io.ent.modules.product.service.ProductTypeService;
import io.ent.modules.sys.entity.SysUserEntity;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2019-10-14 15:50:16
 */
@RestController
@RequestMapping("product/producttype")
public class ProductTypeController {
    @Autowired
    private ProductTypeService productTypeService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("product:producttype:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = productTypeService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("product:producttype:info")
    public R info(@PathVariable("id") Integer id){
        ProductTypeEntity productType = productTypeService.getById(id);

        return R.ok().put("productType", productType);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("product:producttype:save")
    public R save(@RequestBody ProductTypeEntity productType){
        String username = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername();
        productType.setCreater(username);
        productType.setUpdater(username);
        productType.setCreateTime(new Date());
        productType.setUpdateTime(new Date());
        productTypeService.save(productType);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("product:producttype:update")
    public R update(@RequestBody ProductTypeEntity productType){
        ValidatorUtils.validateEntity(productType);
        String username = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername();
        productType.setUpdater(username);
        productType.setUpdateTime(new Date());
        productTypeService.updateById(productType);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("product:producttype:delete")
    public R delete(@RequestBody Integer[] ids){
        productTypeService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 查找所有类型
     */
    @RequestMapping("/getAllProductTypes")
    public R getAllProductTypes(){
        List<ProductTypeEntity> types = productTypeService.getAllProductTypes();

        return R.ok().put("types",types);
    }

    //查找所有产品信息类型---公开的接口
    @RequestMapping("/getProductTypes")
    public R getProductTypes(){
        List<ProductTypeEntityIfPo> types = productTypeService.getProductTypes();
        return R.ok().put("types",types);
    }

}
